package com.example.codegenerator.controller;

import com.example.codegenerator.common.model.dto.GeneratorDTO;
import com.example.codegenerator.common.model.dto.TableLoadDTO;
import com.example.codegenerator.common.model.result.ResponseResult;
import com.example.codegenerator.domain.DatabaseDomainApplication;
import com.example.codegenerator.domain.GeneratorDomainApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * 代码生成 前端控制层
 *
 * @author lvshengyu
 * @date 2023/8/17
 */
@RestController
@RequestMapping("/api/v1")
public class GeneratorController {

    @Autowired
    private DatabaseDomainApplication databaseDomainApplication;

    /**
     * 加载当前数据库所有表
     *
     * @param tableLoadDTO 数据表查询对象
     * @return
     */
    @PostMapping("/load_table")
    public ResponseResult<?> loadTable(@RequestBody TableLoadDTO tableLoadDTO) {
        return ResponseResult.success(databaseDomainApplication.loadTable(tableLoadDTO));
    }

    @Autowired
    private GeneratorDomainApplication generatorDomainApplication;

    /**
     * 代码生成（云端下载方式）
     *
     * @param generatorDTO        代码生成数据传递对象
     * @param httpServletResponse http响应对象
     * @return
     */
    @PostMapping("/generator_download")
    public void generatorCodeDownload(@RequestBody GeneratorDTO generatorDTO, HttpServletResponse httpServletResponse) {
        generatorDomainApplication.generatorCodeDownload(generatorDTO, httpServletResponse);
    }

    /**
     * 应用服务名称
     */
    @Value("${spring.application.name}")
    private String applicationName;

    /**
     * 获取应用服务名称
     *
     * @return
     */
    @PostMapping("/get_application_name")
    public ResponseResult<?> getApplicationName() {
        return ResponseResult.success(applicationName);
    }
}
